#!/usr/bin/env python3
"""
数据库检查脚本
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from database.connection import db_manager

def check_activation_codes():
    """检查激活码"""
    print("🔍 检查激活码...")
    
    try:
        with db_manager.get_connection() as conn:
            cursor = conn.cursor(dictionary=True)
            
            # 检查激活码表
            cursor.execute("SHOW TABLES LIKE 'activation_codes'")
            if not cursor.fetchone():
                print("❌ activation_codes表不存在")
                return
            
            # 查看所有激活码
            cursor.execute("SELECT * FROM activation_codes")
            rows = cursor.fetchall()
            
            print(f"📊 找到 {len(rows)} 个激活码:")
            for row in rows:
                print(f"  - {row['code']}: {row['role']} (有效: {row['is_active']}, 已用: {row['used_by']})")
            
            # 特别检查genglongtao
            cursor.execute("SELECT * FROM activation_codes WHERE code = 'genglongtao'")
            genglongtao = cursor.fetchone()
            
            if genglongtao:
                print(f"\n✅ 找到genglongtao激活码:")
                print(f"  - ID: {genglongtao['id']}")
                print(f"  - 角色: {genglongtao['role']}")
                print(f"  - 有效: {genglongtao['is_active']}")
                print(f"  - 已用: {genglongtao['used_by']}")
                print(f"  - 试用期: {genglongtao['trial_days']}天")
                print(f"  - 订阅期: {genglongtao['subscription_days']}天")
            else:
                print("\n❌ 未找到genglongtao激活码")
                
    except Exception as e:
        print(f"❌ 检查失败: {e}")

def check_users():
    """检查用户"""
    print("\n🔍 检查用户...")
    
    try:
        with db_manager.get_connection() as conn:
            cursor = conn.cursor(dictionary=True)
            
            # 检查用户表
            cursor.execute("SHOW TABLES LIKE 'users'")
            if not cursor.fetchone():
                print("❌ users表不存在")
                return
            
            # 查看所有用户
            cursor.execute("SELECT id, username, role, status, created_at FROM users ORDER BY created_at DESC LIMIT 10")
            rows = cursor.fetchall()
            
            print(f"📊 找到 {len(rows)} 个用户:")
            for row in rows:
                print(f"  - {row['username']}: {row['role']} ({row['status']}) - {row['created_at']}")
                
    except Exception as e:
        print(f"❌ 检查失败: {e}")

if __name__ == "__main__":
    print("🚀 开始数据库检查...")
    check_activation_codes()
    check_users()
    print("\n✅ 检查完成!")
